import api from './apiConfig.js'
import { getWxOpenId,login} from './index.js'
import { getMerchantId } from './merchantacount.js'
/**
 * 发起get请求
 * @param url 请求路径 必填
 * @param data 请求参数 get请求的参数会自动拼到地址后面
 * @param headers 请求头 选填
 * @returns {Promise}
 */
export const get = (url, data, headers) => request('GET', api.base_url + url, data, headers);

/**
 * 发起post请求
 * @param url 请求路径 必填
 * @param data 请求参数
 * @param headers 请求头 选填
 * @returns {Promise}
 */
export const post = (url, data, headers) => request('POST', api.base_url + url, data, headers);
/**
 * 发起put请求
 * @param url 请求路径 必填
 * @param data 请求参数
 * @param headers 请求头 选填
 * @returns {Promise}
 */
export const put = (url, data, headers) => request('PUT', api.base_url + url, data, headers);
/**
 * 发起delete请求
 * @param url 请求路径 必填
 * @param data 请求参数 delete请求的参数会自动拼到地址后面
 * @param headers 请求头 选填
 * @returns {Promise}
 */
export const del = (url, data, headers) => request('DELETE', api.base_url + url, data, headers);

/**
 * 接口请求基类方法
 * @param method 请求方法 必填
 * @param url 请求路径 必填
 * @param data 请求参数
 * @param header 请求头 选填
 * @returns {Promise} 
 */
export async function request(method, url, data, custheader) {
	data = data || {};
	let token = ''
	let merchantId = ''
	if (uni.getStorageSync('token')) {
		token = uni.getStorageSync('token')
	}
	if (uni.getStorageSync('merchantId')) {
		merchantId = uni.getStorageSync('merchantId')
	} else {
		// #ifdef MP-TOUTIAO
		let accountInfo = tt.getEnvInfoSync();
		merchantId = getMerchantId(accountInfo.microapp.appId)
		// #endif

		// #ifdef MP-WEIXIN
		let accountInfo = uni.getAccountInfoSync()
		merchantId = getMerchantId(accountInfo.miniProgram.appId)
		// #endif
		uni.setStorageSync('merchantId', merchantId)
	}
	let header = {
		'Content-Type': custheader ? custheader : 'application/json',
		'auth-type': 'customer',
		'auth-token': token,
		'merchantId': merchantId
	}
	return new Promise((resolve, reject) => {
		const response = {};
		uni.showLoading({
			title: '加载中...',
		})
		uni.request({
			url,
			method,
			data,
			header,
			success: (res) => response.success = res,
			fail: (error) => response.fail = error,
			complete() {
				uni.hideLoading();
				if (response.success) {
					if (response.success.status == '200') {
						resolve(response.success.data)
					} else if (response.success.data.status == '10001') {
						if (url.indexOf('getWxOpenId') == -1 && url.indexOf('getWxPhone') == -1) {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}
						resolve(response.success.data)
					} else {
						resolve(response.success.data)
					}
				} else {
					console.info('请求失败：', response.fail);
					uni.showToast({
						title: "" + response.fail,
						icon: 'none',
						duration: 1000
					})
					reject(response.fail)
				}
			},
		});
	});
}


async function openId(res) {
	return new Promise(function(resolve, reject) {
		getWxOpenId({
			wxcode: res.code
		}).then(resData => {
			console.log('resData.data', resData.data)
			uni.setStorageSync('openId', resData.data)
			login({
				openId: resData.data,
				invitedCode: uni.getStorageSync('invitationCode') ? uni.getStorageSync(
					'invitationCode') : ''
			}).then(res => {
				console.log('res----->', res)
				uni.setStorageSync('userInfo', res.data)
				uni.setStorageSync('token', res.data.authToken)
				resolve(res.data.authToken);
			}).catch(reject);
		}).catch(e => {
			// error
		})
	})
}


function wxLogin() {
	return new Promise(function(resolve, reject) {
		wx.login({
			success(res) {
				if (res.code) {
					console.log('res.code--->', res.code)
					resolve(res)
				} else {
					console.log('登录失败！' + res.errMsg)
					reject(res)
				}
			}
		})
	})

}
